﻿using DAL.Permission.Shop;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BLL.Permission.Shop
{
    public class UserBLL
    {
        private readonly UserDAL dal = new UserDAL();

        /// <summary>
        /// 更新某用户的密码
        /// </summary>
        /// <param name="uname"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool UpdatePwd(int uid, string pwd)
        {
            return dal.UpdatePwd(uid, pwd);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool DisableUser(int uid)
        {
            return dal.DisableUser(uid);
        }

        public UserBLL()
        { }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int UserID)
        {
            return dal.Exists(UserID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(UserEntity model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据 不更新密码
        /// </summary>
        public bool Update(UserEntity model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int UserID)
        {

            return dal.Delete(UserID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string UserIDlist)
        {
            return dal.DeleteList(UserIDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UserEntity GetModel(int UserID)
        {

            return dal.GetModel(UserID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<UserEntity> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<UserEntity> DataTableToList(DataTable dt)
        {
            List<UserEntity> modelList = new List<UserEntity>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                UserEntity model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        /// <summary>
        /// 得到一个对象实体
        /// <param name="loginname">登录名</param>
        /// <param name="password">密码 注意该字段为解密后</param>
        /// </summary>
        public UserEntity GetModel(string loginname, string password)
        {
            return dal.GetModel(loginname, password);
        }

        /// <summary>
        /// 是否存在该记录
        /// <param name="LoginName">登录名</param>
        /// <param name="Password">密码</param>
        /// </summary>
        public int? Exists(string LoginName, string Password)
        {
            return dal.Exists(LoginName, Password);
        }

        /// <summary>
        /// 用户名是否存在
        /// </summary>
        /// <param name="LoginName">登录名</param>
        /// <returns></returns>
        public bool ExistsName(string LoginName)
        {
            return dal.ExistsName(LoginName);
        }

        /// <summary>
        /// 是否存在当前用户名以外的其他用户名
        /// </summary>
        /// <param name="LoginName">登录名</param>
        /// <param name="UserID">密码</param>
        /// <returns></returns>
        public bool ExistsOtherUser(string LoginName, int UserID)
        {
            return dal.ExistsOtherUser(LoginName, UserID);
        }

        /// <summary>
        /// 得到登录权限信息
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public List<LoginModuleEntity> GetUserLogin(int uid, string ip)
        {
            return dal.GetUserLogin(uid, ip);
        }
    }
}